/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.BusinessLogic;
import ejb.Utente;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.Tool;

/**
 *
 * @author Max
 */
public class RegistraUtente extends HttpServlet {
    @EJB
    private BusinessLogic businessLogic;
    

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
            RequestDispatcher rd = null;
            ServletContext sc = getServletContext();
            Utente u = new Utente();
            List<Utente> u2;
            u2 = businessLogic.listaUtenti();
            

            //primo form --DATI ANAGRAFICI--



            String username = request.getParameter("user");
            Tool tool = new Tool();

            if (!u2.isEmpty() && tool.userPresent(username, u2)) {
                rd = sc.getRequestDispatcher("/RegistrazioneUtente.jsp");
            } 
            else {
                u.setNome(request.getParameter("nome"));
                u.setCognome(request.getParameter("cognome"));
                String giorno = request.getParameter("giorno");
                String mese = request.getParameter("mese");
                String anno = request.getParameter("anno");
                u.setDataNascita(giorno + "/" + mese + "/" + anno);

                u.setCittaResidenza(request.getParameter("cittaResidenza"));
                u.setIndirizzo(request.getParameter("indirizzo"));

                String cap = request.getParameter("cap");
                u.setCap(Integer.parseInt(cap));

                //secondo form --DATI ABBIGLIAMENTO--
                String tagliaMaglia = request.getParameter("tagliaMaglia");
                if (tagliaMaglia != null) {
                    u.setTagliaMaglia(tagliaMaglia);
                } else {
                    u.setTagliaMaglia("0");
                }
                String tagliaPantaloni = request.getParameter("tagliaPantaloni");
                if (!tagliaPantaloni.equals("")) {
                    u.setTagliaPantaloni(Integer.parseInt(tagliaPantaloni));
                } else {
                    u.setTagliaPantaloni(0);
                }
                String tagliaScarpe = request.getParameter("tagliaScarpe");
                if (!tagliaScarpe.equals("")) {
                    u.setTagliaScarpe(Integer.parseInt(tagliaScarpe));
                } else {
                    u.setTagliaScarpe(0);
                }

                //terzo form --DATI SITO--
                u.setMail(request.getParameter("mail"));
                u.setUsername(username);
                u.setPassword(util.LMD5Gen.md5(request.getParameter("password")));


                businessLogic.aggiungiUtente(u);

                rd = sc.getRequestDispatcher("/StatoRegistrazione.jsp");

            }
            rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
